package com.xyy.saas.payment.util;

import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.enums.PayModeEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class PayTimeExpireUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PayTimeExpireUtil.class);

    private static final Integer DEFAULT_EXPIRE = 2 * 60 * 60;

    /**
     * 通过渠道、支付方式、超时时间获取 实际超时时间
     * @param channel 支付渠道 <label>ChannelEnum<label/>
     * @param payMode 支付方式 <label>PayModeEnum<label/>
     * @param timeExpire 超时时间
     * @return
     */
    public static Integer handleTimeExpire(String channel, String payMode, Date timeExpire) {
        int expire = DEFAULT_EXPIRE;
        // 微信H5支付方式 - 5分钟H5跳转链接失效
        if(StringUtils.isNotBlank(channel) && channel.contains(Constants.CHANNEL_WX_NAME) && PayModeEnum.H5.getMode().equals(payMode)) {
            expire = 5 * 60;
        }
        if(null != timeExpire && timeExpire.after(DateUtil.now())) {
            // 默认给业务给定的过期时间加上一个半小时
            expire = DateUtil.getSecondsFromToday(timeExpire).intValue() + 5400;
            //expire = DateUtil.getSecondsFromToday(timeExpire).intValue();
            //if (expire > 5400) {
            //    expire = 5400;
            //}
        }
        LOGGER.info("PayTimeExpireUtil#handleTimeExpire success handle time expire. channel:{}, payMode:{}, timeExpire:{}, return expire:{}", channel, payMode, timeExpire, expire);
        return expire;
    }

}